/*
 * Tag.h
 *
 *  Created on: 2010-6-11
 *      Author: guobc
 */

#ifndef TAG_H_
#define TAG_H_
#include <string>
#include <map>
using namespace std;

namespace HA {

typedef map<string, string> Attribute;
/**
 * This class represents a tag in the HTML document.
 */
class Tag {
public:
	/**
	 * Construct tag method whit no argument.
	 */
	Tag();
	/**
	 * Construct tag method with tag name argument.
	 * @param tagName The tag name.
	 */
	Tag(const string& tagName);

	/**
	 * Set the tag name of this tag.
	 * @param tagName The tag name.
	 */
	inline void setTagName(const string& tagName) {
		mTagName = tagName;
	}

	/**
	 * Get the tag name of this tag.
	 * @return The tag name.
	 */
	inline string getTagName() const {
		return mTagName;
	}

	/**
	 * Test whether is a end tag like that '</html>'
	 * @return True if a end tag
	 */
	inline bool isEndTag() const {
		return mTagName.empty() ? false : mTagName.find_first_of("/")
				!= string::npos;
	}

	/**
	 * Test whether has end tag.
	 * Some HTML element is a empty element that has no content and it's
	 * end tag must closing in the start tag.
	 */
	bool hasEndTag();

	/**
	 * Sets attribute with given key and value.
	 * @param key The key of attribute.
	 * @param value The value of attribute.
	 */
	void setAttribute(const string& key, const string& value);

	/**
	 * Gets the value of attribute with given key.
	 * @param key The key of attribute.
	 */
	string getAttribute(const string& key);

	/**
	 * Remove the the attribute with given key, if it exits.
	 * @param key The key of attribute.
	 */
	void removeAttribute(const string& key);

	/**
	 * Return the HTML for this node.
	 * This should be the sequence of characters that were encountered by
	 * the parser that caused this node to be created. Where this breaks down is
	 * where broken nodes (tags and remarks) have been encountered and fixed.
	 * Applications reproducing html can use this method on nodes which are to
	 * be used or transferred as they were received or created.
	 * @return The sequence of characters that would cause this node
	 * to be returned by the parser or lexer.
	 */
	string toHTML();

private:
	string mTagName;
	Attribute attr;
	//this array stores a tag name that has no end tag.
	static string NO_END_TAG_NAMES[];
};

}

#endif /* TAG_H_ */
